package com.hfen;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;

/**
 * @program: websocketnetty
 * @description: main函数，启动类
 * @author: hfen
 * @create: 2021-08-12 10:25
 **/
public class Main {


    /**
     * ServerBootstrap: 引导子类，它允许简单地引导ServerChannel
     * ServerChannel： 接受连接，并创建子通道
     * EventLoop： 为注册的多个通道执行所有的IO操作
     */
    public static void main(String[] args) {

        EventLoopGroup bossGroup = new NioEventLoopGroup();
        NioEventLoopGroup workGroup = new NioEventLoopGroup();

        ServerBootstrap serverBootstrap = new ServerBootstrap();
        serverBootstrap.group(bossGroup, workGroup);
        serverBootstrap.channel(NioServerSocketChannel.class); // 通道类型
        serverBootstrap.childHandler(new MyWebSocketChannelInitializer()); // 注册初始化类
        System.out.println("服务器开启等待客户端连接");
        try {
            Channel channel = serverBootstrap.bind(8800).sync().channel();
            // 当通道关闭时，设置通道将来被通知
            channel.closeFuture().sync();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            bossGroup.shutdownGracefully();
            workGroup.shutdownGracefully();
        }

    }
}
